package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.databinding.BindingAdapter;

import com.sgb.kjwl.R;
import com.swgk.core.customview.ScreenTextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextViewBindingAdapter {

    @BindingAdapter("textForLine")
    public static void setText(TextView text,String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(15,true),s.indexOf("\n"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(150,150,150)),s.indexOf("\n"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setText(spannableString);
    }


    public static SpannableString setForSpace(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(150,150,150)),s.indexOf("   "),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setForLittleWord(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(255,255,255)),s.indexOf("  "),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(10,true),s.indexOf("  "),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new BackgroundColorSpan(Color.rgb(107, 120, 255)),s.indexOf("  "),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setTextForLine(String str){
        SpannableString spannableString = new SpannableString(str);
        if (!str.contains("\n")){
            return spannableString;
        }
        spannableString.setSpan(new AbsoluteSizeSpan(15,true), str.indexOf("\n"), str.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(150,150,150)), str.indexOf("\n"), str.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, str.indexOf("\n"), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setTextForLineRe(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(15,true),s.indexOf("\n"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(150,150,150)),s.indexOf("\n"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, s.indexOf("\n"), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setTextForColon(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(51,51,51)),s.indexOf(":"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setTextForColon2(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(51,51,51)),s.indexOf(":"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setTextForPoint(String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(15,true),s.indexOf("."),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static boolean chinaCheck(String str){
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    // 判断一个字符是否是中文
    private static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
    }

    // 判断一个字符串是否含有中文
    public static boolean isChinese(String str) {
        if (str == null)
            return false;
        for (char c : str.toCharArray()) {
            if (isChinese(c))
                return true;// 有一个中文字符就返回
        }
        return false;
    }


    public static SpannableString setForLittleMoney(String s){
        SpannableString spannableString = new SpannableString(s);
        if (!s.equals("议价")){
            spannableString.setSpan(new AbsoluteSizeSpan(14,true),0,s.indexOf("￥"),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    @BindingAdapter("android:layout_width")
    public static void setLayoutWidth(View view, float width) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = (int) width;
        view.setLayoutParams(params);
    }

    public static SpannableString setMoney(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(12,true),s.indexOf("."),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    @BindingAdapter("screenTextViewContent")
    public static void setScreenTextViewContent(ScreenTextView view, String width) {
        view.setText(width);
    }

    public static SpannableString setNoddleLine(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new StrikethroughSpan(),0,s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setMoneyForRMB(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(14,true), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(16,true), 1, s.indexOf("."), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(14,true), s.indexOf("."), s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setMoneyForRMB18(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(16,true), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(18,true), 1,s.indexOf("."),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(16,true),s.indexOf("."),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableString setLabelName(String s) {
        SpannableString spannableString = new SpannableString(s);
        if (!s.contains("*"))
            return spannableString;
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, s.indexOf("*"), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(15,true),0, s.indexOf("*"), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF2928")),  s.indexOf("*"), s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(12,true), s.indexOf("*"), s.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    @BindingAdapter("textForStar")
    public static void setTextForStar(TextView text,String s){
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new ForegroundColorSpan(Color.rgb(255,41,40)),s.indexOf("*"),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setText(spannableString);
    }
}
